
Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'side';

Ext.ns('X.field');

//----------------------------//
//       X.field.Hidden       //
//----------------------------//

X.field.Hidden = Ext.extend(Ext.form.Hidden, {
});

//--------------------------//
//       X.field.Text       //
//--------------------------//

X.field.Text = Ext.extend(Ext.form.TextField, {
    allowBlank : true
});

//--------------------------//
//       X.field.Area       //
//--------------------------//

X.field.Area = Ext.extend(Ext.form.TextArea, {
    allowBlank : true
});

//---------------------------//
//       X.field.Email       //
//---------------------------//

X.field.Email = Ext.extend(Ext.form.TextField, {
    allowBlank : true,
    vtype      : 'email',
    vtypeText  : 'email inválido'
});

//---------------------------//
//       X.field.Url         //
//---------------------------//

X.field.Url= Ext.extend(Ext.form.TextField, {
    allowBlank : true,
    vtype      : 'url',
    vtypeText  : 'Dirección inválida. Debe tener la forma http://www.ejemplo.com'
});

//----------------------------//
//       X.field.Number       //
//----------------------------//

X.field.Number = Ext.extend(Ext.form.NumberField, {
    allowBlank : true
});

//--------------------------//
//       X.field.Date       //
//--------------------------//

X.field.Date = Ext.extend(Ext.form.DateField, {
    allowBlank : true,
    format     : 'd/m/Y',
    altFormats : 'd/m/Y|Y-m-d H:i:s|c',
    invalidText: 'fecha inválida'
});

//--------------------------//
//       X.field.Pass       //
//--------------------------//

X.field.Pass = Ext.extend(Ext.form.TextField, {
    allowBlank : true,
    inputType  : 'password',
    validator  : function(value){
        // si es password
        if (this.checkedBy){
            if (Ext.getCmp(this.checkedBy).getValue() != ''){
                Ext.getCmp(this.checkedBy).validate();
            }
        }
        //si es confirm
        if (this.checkPass){
            if (value == '') return true;
            if (Ext.getCmp(this.checkPass).getValue() == value) return true;
            return 'no son iguales';
        }
        return true;
    }
});

//---------------------------//
//       X.field.Combo       //
//---------------------------//

X.field.Combo = Ext.extend(Ext.form.ComboBox, {
    editable      : false,
    mode          : 'local',
    triggerAction : 'all',
    allowBlank    : true
});


//----------------------------//
//       X.field.Checkbox      //
//----------------------------//

X.field.Checkbox = Ext.extend(Ext.form.Checkbox, {
});


//----------------------------//
//       X.field.Radiogroup    //
//----------------------------//

X.field.RadioGroup = Ext.extend(Ext.form.RadioGroup, {
});


//------------------------------//
//       X.form.FormPanel       //
//------------------------------//

Ext.ns('X.form');

X.form.FormPanel = Ext.extend(Ext.form.FormPanel, {

    renderTo     : 'layout_content',
    bodyStyle    : 'padding:20px 20px 20px 20px;',
    style        : 'margin:auto; textAlign:left;',
    frame        : true,
    monitorValid : false,

    initComponent : function() {

        // pasar el handler a los items
        for (var i=0; i<this.items.length; i++)
        {
            this.items[i].form = this;
        }

        //====== BUTTONS ======//

        var submitFunction = function()
        {
            this.getForm().submit({
                url       : this.submitUrl,
                waitTitle : 'Aguarde un momento.',
                waitMsg   : 'Cargando...',

                success : function(form, action){
                    try{
                        if (action.result.msg){
                            X.msg.SuccessRedirect(action.result.msg, this.returnUrl);
                        }
                        else{ window.location = this.returnUrl;}
                    }
                    catch(e){
                        X.msg.FailureJs(e.message);
                    }
                }.bind(this),
                failure : function(form, action){
                    try{
                        if (action.failureType === Ext.form.Action.SERVER_INVALID){
                            X.msg.Failure(action.result.msg);
                        }
                        else X.msg.Failure('respuesta inválida del servidor');
                    }
                    catch(e){
                        X.msg.FailureJs(e.message);
                    }
                }
            });
        };

        var submitButton = !this.submitButtonText? null : new Ext.Button({
            text     : this.submitButtonText,
            formBind : true,
            handler  : submitFunction.bind(this)
        });
        
        var returnButton = !this.returnButtonText? null : new Ext.Button({
            text    : this.returnButtonText,
            handler : function(){
                window.location = this.returnUrl;
            }.bind(this)
        });

        this.buttons = [];
        if (submitButton) this.buttons.push(submitButton);
        if (returnButton) this.buttons.push(returnButton);

        //====== PARENT CONSTRUCTOR ======//

        X.form.FormPanel.superclass.initComponent.call(this);
    }
});

X.form.FormPanel2 = Ext.extend(Ext.form.FormPanel, {

    renderTo     : 'layout_content',
    bodyStyle    : 'padding:20px 20px 20px 20px;',
    style        : 'margin:auto; textAlign:left;',
    frame        : true,
    monitorValid : false,


    initComponent : function() {

        // pasar el handler a los items
        for (var i=0; i<this.items.length; i++)
        {
            this.items[i].form = this;
        }

        //====== BUTTONS ======//

        var submitFunction = function()
        {
            this.getForm().submit({
                url       : this.submitUrl,
                waitTitle : 'Aguarde un momento.',
                waitMsg   : 'Cargando...',

                success : function(form, action){
                    try{
                        if (action.result.msg){
                            X.msg.Success(action.result.msg);
                        }
                        new Ext.ux.MediaWindow({
                                id        : 'PDFViewerWin',
                                title     : 'PDF',
                                bodyStyle : 'position:relative; padding:0px;',
                                width     : 900,
                                height    : 600,
                                mediaCfg  : {
                                    mediaType       : 'PDF',
                                    url             : this.returnUrl,
                                    unsupportedText : 'Acrobat Viewer is not Installed',
                                    resizable       : true
                                }
                            }).show(); 
                    }
                    catch(e){
                        X.msg.FailureJs(e.message);
                    }
                }.bind(this),
                failure : function(form, action){
                    try{
                        if (action.failureType === Ext.form.Action.SERVER_INVALID){
                            X.msg.Failure(action.result.msg);
                        }
                        else X.msg.Failure('respuesta inválida del servidor');
                    }
                    catch(e){
                        X.msg.FailureJs(e.message);
                    }
                }
            });
        };

        var submitButton = !this.submitButtonText? null : new Ext.Button({
            text     : this.submitButtonText,
            formBind : true,
            handler  : submitFunction.bind(this)
        });

        var returnButton = !this.returnButtonText? null : new Ext.Button({
            text    : this.returnButtonText,
            handler : function(){
                window.location = '/';
            }.bind(this)
        });

        this.buttons = [];
        if (submitButton) this.buttons.push(submitButton);
        if (returnButton) this.buttons.push(returnButton);

        //====== KEYS ======//

        var keyFunction = function(){
            if(!this.disabled)
				this.handler();
        };

        this.keys = [];
        if (submitButton) this.keys.push({key: 13, handler: keyFunction.bind(submitButton)});
        if (returnButton) this.keys.push({key: 27, handler: keyFunction.bind(returnButton)});

        //====== PARENT CONSTRUCTOR ======//

        X.form.FormPanel.superclass.initComponent.call(this);
    }
});